/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package raytracer;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Time;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import sun.net.www.content.image.png;

/**
 *
 * @author ferco
 * Trida pro propojeni RayCasteru s obrazovkou. Rozsiruje Frame, vytvari Image
 * screen ve kterem se ukladaji hodnoty z paprsku. Tento screen pote zobrazi.
 */
public class RayRenderer extends Frame{

    //deklarece promennych
    private BufferedImage screen;

    /**
     * Konstruktor, nastavi vsechno potrebne a spusti raycaster
     * @param scene
     */
    public RayRenderer(Scene scene){

        //konsturktor z predka..predava se nazev framu
        super("RayTracing frame");

        //vytvoreni mediatrackeru..zjistit co presne dela
        MediaTracker mediaTracker = new MediaTracker(this);

        //nastaveni velikosti okna
        this.setSize(250, 200);
        

        //vytvoreni screen, parametry..sirka,vyska,barevny typ
        screen = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);

        //resetovani screenu
        this.clearImage();

        //vytvoreni raycasteru
        RayCaster rayCaster = new RayCaster(screen, scene);

        long firstTime = System.currentTimeMillis();
        //zacne vrhat paprsky
        rayCaster.castRays();

        long secondTime = System.currentTimeMillis();
        Time renderTime = new Time(secondTime-firstTime);
        System.out.println(secondTime-firstTime);
        // do mediatrackeru pridame screen,ktery uz obsahuje hodnoty z paprsku
        mediaTracker.addImage(screen, 0);

        //System.currentTimeMillis();
        this.saveImage(System.currentTimeMillis() + ".png");
        //zobrazime frame
        this.setVisible(true);
        

        //nastaveni akce pro ukonceni
        addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent we){
            dispose();
            }
        });

    }

    /**
     * nastavi vsechnz pixely ve screen na bilou barvu
     */
    public void clearImage(){

      for (int i = 0; i < screen.getWidth(); i++){
            for (int j = 0; j < screen.getHeight(); j++) {
                    screen.setRGB(i, j, Color.WHITE.getRGB());

            }
        }
  }


    public void saveImage(String name){
        try {
            //new File(name);
            ImageIO.write(screen, "png", new File(name));
        } catch (IOException ex) {
            System.out.print(ex);
        }
    }

    @Override
  public void update(Graphics g){
    paint(g);
  }

    @Override
  public void paint(Graphics g){
    if(screen != null)
      g.drawImage(screen, 0, 0, this);
    else
      g.clearRect(0, 0, getSize().width, getSize().height);
  }


}
